﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Kaca.Core.Common;

namespace Kunlun.BusinessLogic
{
    public class KunlunCookies
    {
		private static readonly string HIS_COOKIS = "his";
		private static readonly string CAR_COOKIS = "car";          //购物车的cookie名称
        private static int _expirestime = 2400;
        public static int Expirestime { set { _expirestime = value; } }

        ////
       /// <summary>
        /// 写网站cookie值
       /// </summary>
        /// <param name="strName">项</param>
        /// <param name="strValue">值</param>
       /// <param name="domain">域名</param>
       /// <param name="expiresCount">过期时间</param>
        public static void WriteCookie(string strName, string strValue, string domain, int expiresCount)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["kunlun"];
            if (cookie == null)
            {
                cookie = new HttpCookie("kunlun");
                cookie.Values[strName] = HttpUtility.UrlEncode(strValue);
                if (expiresCount > 0)
                {
                    cookie.Expires = DateTime.Now.AddMinutes(_expirestime > expiresCount ? _expirestime : expiresCount);
                }
                else
                {
                    cookie.Expires = DateTime.Now.AddMinutes(expiresCount);
                }
            }
            else
            {
                cookie.Values[strName] = HttpUtility.UrlEncode(strValue);
                if (HttpContext.Current.Request.Cookies["kunlun"]["expires"] != null)
                {
                    int expires = Utils.StrToInt(HttpContext.Current.Request.Cookies["kunlun"]["expires"].ToString(), 0);
                    if (expires > 0)
                    {
                        cookie.Expires = DateTime.Now.AddMinutes(expires);
                    }
                }
            }

            HttpContext.Current.Response.AppendCookie(cookie);
        }

        /// <summary>
        /// 写网站cookie值
        /// </summary>
        /// <param name="strName">项</param>
        /// <param name="strValue">值</param>
        public static void WriteCookie(string strName, string strValue)
        {
            WriteCookie(strName, strValue, "",_expirestime);
        }

        /// <summary>
        /// 获得cookie值
        /// </summary>
        /// <param name="strName">项</param>
        /// <returns>值</returns>
        public static string GetCookie(string strName)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies["kunlun"] != null && HttpContext.Current.Request.Cookies["kunlun"][strName] != null)
            {
                return HttpUtility.HtmlDecode(HttpContext.Current.Request.Cookies["kunlun"][strName].ToString());
            }

            return "";
        }

        /// <summary>
        /// 清除论坛登录用户的cookie
        /// </summary>
        public static void ClearUserCookie()
        {
            ClearUserCookie("kunlun");
        }

        public static void ClearUserCookie(string cookieName)
        {
            HttpCookie cookie = new HttpCookie(cookieName);
            cookie.Values.Clear();
            cookie.Expires = DateTime.Now.AddYears(-1);
            HttpContext.Current.Response.AppendCookie(cookie);
        }

		public static HttpCookie GetHisCookis() {
			HttpCookie cookie = null;
			if (HttpContext.Current.Request.Cookies[HIS_COOKIS] == null) {
				cookie = new HttpCookie(HIS_COOKIS);
				cookie.Value = "";
				cookie.Expires = DateTime.Now.AddMonths(1);
				
			} else {
				cookie = HttpContext.Current.Request.Cookies[HIS_COOKIS];
			}
			return cookie;
		}

		public static void ClearHisCookis() {
			HttpCookie cookie = new HttpCookie(HIS_COOKIS);
			cookie.Values.Clear();
			cookie.Expires = DateTime.Now.AddYears(-1);
			HttpContext.Current.Response.AppendCookie(cookie);
		}

        #region  购物车cookie
        /// <summary>
        /// 获取购物车的cookie
        /// </summary>
        public static HttpCookie GetCarCookis()
        {
            HttpCookie cookie = null;
            if (HttpContext.Current.Request.Cookies[CAR_COOKIS] == null)
            {
                cookie = new HttpCookie(CAR_COOKIS);
                cookie.Value = "";
                cookie.Expires = DateTime.Now.AddDays(7);       //7天的过期时间
            }
            else
            {
                cookie = HttpContext.Current.Request.Cookies[CAR_COOKIS];
            }
            return cookie;
        }

        /// <summary>
        /// 清除购物车的cookie
        /// </summary>
        public static void ClearCarCookis()
        {
            HttpCookie cookie = new HttpCookie(CAR_COOKIS);
            cookie.Values.Clear();
            cookie.Expires = DateTime.Now.AddYears(-1);
            HttpContext.Current.Response.AppendCookie(cookie);
            HttpContext.Current.Request.Cookies.Remove(CAR_COOKIS);
        }
        #endregion

        public static void ResetCookis(HttpCookie cookie) {
			HttpContext.Current.Response.Cookies.Set(cookie);
			HttpContext.Current.Request.Cookies.Set(cookie);
		}

		
    }
}
